package haizhi.fruitmall.service.impl;

import haizhi.fruitmall.dao.mapper.UserMapper;
import haizhi.fruitmall.enumeration.GoodsCommentEnum;
import haizhi.fruitmall.enumeration.GoodsPictureEnum;
import haizhi.fruitmall.pojo.form.*;
import haizhi.fruitmall.pojo.po.*;
import haizhi.fruitmall.pojo.view.*;
import haizhi.fruitmall.service.*;
import haizhi.fruitmall.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Date: 2017/12/23 17:06
 * @Author: Richard Zane
 */

@Service
@Transactional
public class DetailServiceImpl implements DetailService{


    @Autowired
    private GoodsUnitService goodsUnitService;

    @Autowired
    private GoodsParamService goodsParamService;

    @Autowired
    private GoodsParamTypeService goodsParamTypeService;

    @Autowired
    private GoodsParamJoinService goodsParamJoinService;

    @Autowired
    private GoodsPictureService goodsPictureService;

    @Autowired
    private GoodsCommentService goodsCommentService;

    @Autowired
    private UserMapper userMapper;

    //接收unitId
    @Override
    public GoodsAndParamView getParam(Long unitId){
        GoodsAndParamView goodsAndParamView = new GoodsAndParamView();
        GoodsUnit goodsUnit = goodsUnitService.getone(unitId);
        BeanUtil.copyBean(goodsUnit, goodsAndParamView);
        GoodsParamJoinForm goodsParamJoinForm = new GoodsParamJoinForm();
        goodsParamJoinForm.setUnitId(unitId);
        goodsParamJoinForm.setIsChoose(true);
        List<GoodsParamJoin> goodsParamJoins = goodsParamJoinService.getall(goodsParamJoinForm);
        List<Long> paramIds = new ArrayList<>();
        for(GoodsParamJoin goodsParamJoin : goodsParamJoins){
            paramIds.add(goodsParamJoin.getParamId());
        }
        List<String> paramValues = new ArrayList<>();
        List<GoodsParam> goodsParamList = goodsParamService.getall(new GoodsParamForm(paramIds));
        for(GoodsParam goodsParam : goodsParamList){
           paramValues.add(goodsParam.getParamValue());
        }
        goodsAndParamView.setParamValues(paramValues);
        return goodsAndParamView;
    }

    //接收商品编号goodsId
    @Override
    public Map getHead(Long goodsId){
        Map<String,Object> dataMap = new LinkedHashMap<>();
        ////商品选择图片choosePictureViewList和详情图片detailPictureViewList
        List<GoodsPictureView> choosePictureViewList = new ArrayList<>();
        List<GoodsPictureView> detailPictureViewList = new ArrayList<>();
        List<GoodsPicture> goodsPictureList = goodsPictureService.getall(new GoodsPictureForm(goodsId));
        for(GoodsPicture goodsPicture : goodsPictureList){
            GoodsPictureView goodsPictureView = new GoodsPictureView();
            BeanUtil.copyBean(goodsPicture, goodsPictureView);
            if(goodsPicture.getPictureType().equals(GoodsPictureEnum.CHOOSE_PICTURE.getCode())){
                choosePictureViewList.add(goodsPictureView);
            }else{
                detailPictureViewList.add(goodsPictureView);
            }
        }
        Map<String,Object> goodsPictureMap = new LinkedHashMap<>();
        goodsPictureMap.put("choosePicture",choosePictureViewList);
        goodsPictureMap.put("detailPicture",detailPictureViewList);
        dataMap.put("goodsPicture",goodsPictureMap);


        ////商品单元goodsUnitViewList
        List<GoodsUnitView> goodsUnitViewList = new ArrayList<>();
        Map<Long,List<Long>> goodsUnitParamJoinMap = new HashMap<>();
        List<GoodsUnit> goodsUnitList = goodsUnitService.getall(new GoodsUnitForm(goodsId));
        for(GoodsUnit goodsUnit : goodsUnitList) {
            GoodsUnitView goodsUnitView = new GoodsUnitView();
            BeanUtil.copyBean(goodsUnit, goodsUnitView);
            goodsUnitViewList.add(goodsUnitView);
            goodsUnitParamJoinMap.put(goodsUnit.getUnitId(),new ArrayList<Long>());
        }
        dataMap.put("goodsUnit",goodsUnitViewList);


        ////商品选择参数chooseParamViewList和详情参数detailParamViewList
        Map<Long,GoodsParamJoin> paramIdJoins = new HashMap<>();
        List<GoodsParamJoin> goodsParamJoinList = goodsParamJoinService.getall(new GoodsParamJoinForm(goodsId));
        for(GoodsParamJoin goodsParamJoin : goodsParamJoinList){
            paramIdJoins.put(goodsParamJoin.getParamId(),goodsParamJoin);
            if(goodsUnitParamJoinMap.containsKey(goodsParamJoin.getUnitId())){
                List<Long> paramIds = goodsUnitParamJoinMap.get(goodsParamJoin.getUnitId());
                paramIds.add(goodsParamJoin.getParamId());
                goodsUnitParamJoinMap.put(goodsParamJoin.getUnitId(),paramIds);
            }
        }
        List<GoodsParam> goodsParamList = goodsParamService.getall(new GoodsParamForm(new ArrayList<>(paramIdJoins.keySet())));
        Map<Long,Integer> paramIdTypeIds = new HashMap<>();
        for(GoodsParam goodsParam : goodsParamList){
            paramIdTypeIds.put(goodsParam.getParamId(),goodsParam.getTypeId());
        }
        List<GoodsParamType> goodsParamTypeList = goodsParamTypeService.getall(new GoodsParamTypeForm(new ArrayList<>(paramIdTypeIds.values())));
        List<Integer> typePids = new ArrayList<>();
        Map<Integer,GoodsParamType> goodsParamTypeMap = new HashMap<>();
        for(GoodsParamType goodsParamType : goodsParamTypeList){
            typePids.add(goodsParamType.getTypePid());
            goodsParamTypeMap.put(goodsParamType.getTypeId(),goodsParamType);
        }
        List<GoodsParamType> parentTypeList = goodsParamTypeService.getall(new GoodsParamTypeForm(typePids));
        Map<Integer,String> parentTypeMap = new HashMap<>();
        for(GoodsParamType parentType : parentTypeList){
            parentTypeMap.put(parentType.getTypeId(),parentType.getTypeName());
        }
        List<GoodsParamView> chooseParamViewList = new ArrayList<>();
        List<GoodsParamView> detailParamViewList = new ArrayList<>();
        for(GoodsParam goodsParam : goodsParamList){
            GoodsParamType goodsParamType = goodsParamTypeMap.get(paramIdTypeIds.get(goodsParam.getParamId()));
            GoodsParamView goodsParamView = new GoodsParamView(goodsParamType.getTypeName());
            BeanUtil.copyBean(goodsParam,goodsParamView);
            if(paramIdJoins.get(goodsParam.getParamId()).getIsChoose()){
                chooseParamViewList.add(goodsParamView);
            }else{
                goodsParamView.setTypePname(parentTypeMap.get(goodsParamType.getTypePid()));
                detailParamViewList.add(goodsParamView);
            }
        }
        Map<String,Object> goodsParamMap = new LinkedHashMap<>();
        goodsParamMap.put("chooseParam",chooseParamViewList);
        goodsParamMap.put("detailParam",detailParamViewList);
        dataMap.put("goodsParam",goodsParamMap);

        ////商品参数关联
        List<GoodsParamJoinView> goodsParamJoinViewList = new ArrayList<>();
        for(Map.Entry<Long,List<Long>> entry : goodsUnitParamJoinMap.entrySet()){
            GoodsParamJoinView goodsParamJoinView = new GoodsParamJoinView();
            goodsParamJoinView.setUnitId(entry.getKey());
            goodsParamJoinView.setParamIds(entry.getValue());
            goodsParamJoinViewList.add(goodsParamJoinView);
        }
        dataMap.put("paramJoin", goodsParamJoinViewList);

        return dataMap;
    }

    //接收goodsId,pageNum,pageSize
    @Override
    public Map getTail(GoodsCommentForm goodsCommentForm){
        Map<String,Object> dataMap = new LinkedHashMap<>();
        ////商品总体评论
        Long totalCount = goodsCommentService.getCount(goodsCommentForm);
        Long greatCount = goodsCommentService.getCount(new GoodsCommentForm(GoodsCommentEnum.GREATE.getCode()));
        BigDecimal greatRate = new BigDecimal(greatCount).divide(new BigDecimal(totalCount),2,BigDecimal.ROUND_HALF_DOWN);
        Map<String,Object> generalCommentMap = new LinkedHashMap<>();
        generalCommentMap.put("totalCount",totalCount);
        generalCommentMap.put("greatRate",BeanUtil.toPercent(greatRate,2));
        dataMap.put("generalComment",generalCommentMap);
        ////商品详情评论
        //封装typeIdNameMap
        Map<Integer,String> typeIdNameMap = new HashMap<>();
        List<GoodsCommentEnum> goodsCommentEnumList = Arrays.asList(GoodsCommentEnum.WORSE, GoodsCommentEnum.COMMON, GoodsCommentEnum.GREATE);
        for(GoodsCommentEnum goodsCommentEnum : goodsCommentEnumList){
            typeIdNameMap.put(goodsCommentEnum.getCode(), goodsCommentEnum.getHint());
        }
        //封装commentUserIdMap,userIdNameMap
        Map<Long,Integer> commentUserIdMap = new HashMap<>();
        List<GoodsComment> goodsCommentList = goodsCommentService.getall(goodsCommentForm);
        for(GoodsComment goodsComment : goodsCommentList){
            commentUserIdMap.put(goodsComment.getCommentId(),goodsComment.getUserId());
        }
        Map<Integer,String> userIdNameMap = new HashMap<>();
        UserExample userExample = new UserExample();
        userExample.or().andUserIdIn(new ArrayList<>(commentUserIdMap.values()));
        List<User> userList = userMapper.selectByExample(userExample);
        for(User user : userList) {
            userIdNameMap.put(user.getUserId(), user.getUserName());
        }
        List<GoodsCommentView> goodsCommentViewList = new ArrayList<>();
        for(GoodsComment goodsComment: goodsCommentList){
            GoodsCommentView goodsCommentView = new GoodsCommentView();
            BeanUtil.copyBean(goodsComment, goodsCommentView);
            goodsCommentView.setTypeName(typeIdNameMap.get(goodsComment.getTypeId()));
            goodsCommentView.setUserName(userIdNameMap.get(commentUserIdMap.get(goodsCommentView.getCommentId())));
            goodsCommentViewList.add(goodsCommentView);
        }
        dataMap.put("detailComment", goodsCommentViewList);
        return dataMap;
    }

}
